using System.Collections;
using Script.Library.Ability;
using UnityEngine;

public class Spiderweb : MonoBehaviour
{
	public new GameObject gameObject;

	public new Transform transform;

	public CircleCollider2D circleCollider;

	private bool activated;

	private Cobweb.Data data;

	private const float posZ = 0.004f;

	public void Begin(Creature creature, Cobweb.Data data)
	{
		this.data = data;
		transform.localScale = data.scale;
		Vector3 positionOfTheEndOfTheBody = creature.GetPositionOfTheEndOfTheBody(circleCollider.bounds.extents.x);
		positionOfTheEndOfTheBody.z = 0.004f;
		transform.position = positionOfTheEndOfTheBody;
		Invoke("TryDestroy", data.timeOfLife);
	}

	private void OnTriggerEnter2D(Collider2D collision)
	{
		if (!activated)
		{
			Creature creatureFromMeat = Info.GetCreatureFromMeat(collision);
			if ((bool)creatureFromMeat && !creatureFromMeat.webWalker)
			{
				StartCoroutine(Trap(creatureFromMeat));
			}
		}
	}

	private IEnumerator Trap(Creature creature)
	{
		activated = true;
		Object.Destroy(circleCollider);
		creature.LockAbilities();
		creature.BeginBlockingMovement();
		yield return new WaitForSeconds(data.duration);
		if ((bool)creature)
		{
			creature.UnlockAbilities();
			creature.EndBlockingMovement();
		}
		Object.Destroy(gameObject);
	}

	private void TryDestroy()
	{
		if (!activated)
		{
			Object.Destroy(gameObject);
		}
	}
}
